package cn.iocoder.yudao.module.system.dal.redis.common;


import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.time.Duration;

import static cn.iocoder.yudao.module.system.dal.redis.RedisKeyConstants.CAPTCHA_CODE;

/**
 * 统计计数redis
 */
@Repository
public class CountRedisDAO {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public String get(String business,String id) {
        String redisKey = formatKey(business+"_"+id);
        return stringRedisTemplate.opsForValue().get(redisKey);
    }

    public void set(String business, String id,Integer count, Duration timeout) {
        String redisKey = formatKey(business+"_"+id);
        stringRedisTemplate.opsForValue().set(redisKey, count.toString(), timeout);
    }

    public void delete(String business,String id) {
        String redisKey = formatKey(business+"_"+id);
        stringRedisTemplate.delete(redisKey);
    }

    private static String formatKey(String key) {
        return String.format(CAPTCHA_CODE.getKeyTemplate(), key);
    }

}
